package com.qf.dao;

import com.qf.pojo.BookInfo;
import com.qf.pojo.BookType;
import com.qf.utils.JpaUtils;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import java.util.Set;

public class BookTypeDao {
    public static void main(String[] args) {
    BookTypeDao dao=new BookTypeDao();
    //dao.findById(1L);
       // dao.addType();
        dao.find2();
    }
    public void addType(){
        EntityManager session = JpaUtils.getSession();
        EntityTransaction transaction = session.getTransaction();
        transaction.begin();
        BookType pojo=new BookType();
        pojo.setTypeName("武侠类");
        pojo.setTypeDesc("仗剑天涯，锄强扶弱，愧意恩仇。。。");
        session.persist(pojo);
        transaction.commit();
        session.close();
    }

    public BookType findById(Long typeId){
        EntityManager session = JpaUtils.getSession();
        BookType bookType = session.find(BookType.class, typeId);
        System.out.println("图书分类："+bookType.getTypeName()+"\t"+bookType.getTypeDesc());
        session.close();
        return bookType;
    }

    public void find2(){
        EntityManager session = JpaUtils.getSession();
        BookType bookType = session.find(BookType.class, 1L);
        System.out.println("图书分类："+bookType.getTypeName()+"\t"+bookType.getTypeDesc());
        Set<BookInfo> bookInfos = bookType.getBookInfos();
        for (BookInfo bookInfo : bookInfos) {
            System.out.println("包含的图书："+"《"+bookInfo.getBookName()+"》"+"\t"+bookInfo.getBookDesc()+"\t"+bookInfo.getBookAuthor());
        }
        session.close();
    }
}
